package cn.com.Servlet.VipType;

import cn.com.controler.VipType.VipTypeDAO;
import cn.com.entity.VipType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "VipTypeServlet",value = "/VipType")
public class VipTypeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        VipTypeDAO vipTypeDAO = new VipTypeDAO();
        if(type.equals("addtype")){      //添加类型
            String vipname = request.getParameter("vipname");
            String viptime = request.getParameter("viptime");
            String vipprice = request.getParameter("vipprice");
            String vipstate = request.getParameter("vipstate");
            //封装了一个vip类型对象
            VipType vipType = new VipType(1,vipname,Integer.valueOf(viptime),Double.valueOf(vipprice),vipstate);
            int i = vipTypeDAO.addViptype(vipType);
            if(i > 0){
                response.sendRedirect("/VipType?type=alltype");
            }else{
                response.sendRedirect("/WebContent/Public/error/error.jsp");
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        VipTypeDAO vipTypeDAO = new VipTypeDAO();
        if(type.equals("alltype")){      //查询所有类型
            List list = vipTypeDAO.selectVipType();
            request.setAttribute("alltypes",list);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_viptype/Showviptype.jsp").forward(request,response);
        }else if(type.equals("delviptype")){    //删除时查询所有
            List list = vipTypeDAO.selectVipType();
            request.setAttribute("alltypes",list);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_viptype/DeleteVipType.jsp").forward(request,response);
        }else if(type.equals("delbyid")){     //删除
            Integer id = Integer.valueOf(request.getParameter("id"));
            int i = vipTypeDAO.deleteVipTypeByid(id);
            if(i > 0){
                response.sendRedirect("/VipType?type=delviptype");
            }else{
                response.sendRedirect("/VipType?type=delviptype");
            }
        }
    }
}
